home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / c_wndw.zip / TEXTEDIT.C < prev    next >
Text File  |  1989-05-25  |  9KB  |  286 lines

  1. /*     (c) Marietta Systems, Inc. 1987
  2. *    All rights reserved
  3. */
  4. #include "mtest.h"
  5. /*
  6. *
  7. *    Program to demonstrate use of ascii files
  8. *    A simple text editor
  9. *
  10. */
  11. unsigned maxsize = 20000;
  12. unsigned maxrecs = 253;
  13. void delline(int*, int, int);
  14. void editfile(char*, int*, int*);
  15. int  getfile(char*, char*, int*, int*);
  16. void insline(int*, int, int);
  17. void linedisp(char*, int*, int, int);
  18. void putfile(int, char*, char*, int*, int); 
  19. void scrndisp(char*, int*, int, int*, int);
  20. void show_nbr(int, int);
  21. /*
  22. *
  23. *    Function to obtain file and read it in
  24. *
  25. */
  26. int getfile(fname, text, lineptr, len)
  27. char *fname, *text;
  28. int *lineptr;
  29. int *len;{
  30.     long rec_nbr;
  31.     int z, slen, fh, ret;
  32.     byte out[20], *ptr;
  33.   /* setup */
  34.     strcpy(fname, "textfile.dat");
  35.     top_spot(1);
  36.     for (z = 0 ; z < 8 ; KEYMATCH[z++] = 0); /* blank out function keys */
  37.     disp_err("",1); /* display new function key settings */
  38.     for (z = 0, slen = 0 ; z < maxrecs; z++, slen += 79)
  39.          {lineptr[z] = slen; text[slen] = 0;}/* initialize out line ptr */
  40.   /*    */
  41.     for(;;){
  42.         display("Text file to edit -  ", 19, 1, low);
  43.         if (accept(fname, left, alt_reverse, 51, 0) && INCHAR == QUIT)
  44.         goodbye(0);
  45.         if ((fh = fileopen(fname, ascii, readonly)) < 0)
  46.             {disp_err("File name or access error",1); continue;}
  47.         disp_err(fname, 1);
  48.         if (!fh) if (!disp_qry("File not found, Create")) continue;
  49.             else {*len = 0; ptr = fname; 
  50.             for (z = 0 ; z < strlen(fname) && fname[z] != '.' ; z++)
  51.                 if (fname[z] == '\\') ptr = &fname[z+1];
  52.             display(ptr, 1, 50, reverse); 
  53.             return 0;}
  54.         display((byte*)FN[fh].fname, 1, 50, reverse);
  55.         fileseek(fh, -1L); /* position to end of file */
  56.         if (FN[fh].location >= (long)maxsize)
  57.             {disp_err("File is too big for editor", 1); 
  58.             fileclos(fh); continue;}
  59.         fileinit(fh, 0, 81, 0L);  /* Max record width is set to 80 */
  60.         *len = 0; text[0] = 0; 
  61.         if (!fh) return 0; /* new text file */
  62.       /* read in the text and set up line pointers */
  63.         rec_nbr = 0L;
  64.         if (fileread(fh, firstrec, &rec_nbr) <= 0) goodbye(10);
  65.         ptr = text;
  66.         display ("Loading text file ", 21, 1, high); 
  67.         for (; rec_nbr < (long)maxrecs;){
  68.             slen = strlen(FN[fh].record);
  69.             FN[fh].record[slen-1] = 0;  /* replace the '\n' with 0 */
  70.             strncpy(ptr, FN[fh].record, 78); ptr = &ptr[79];
  71.             if ((ret = fileread(fh, nextrec, &rec_nbr)) < 0) 
  72.                 goodbye(11);
  73.             if (!ret) {*len = (int)rec_nbr - 2; 
  74.                 fileclos(fh); return fh;} /* EOF */
  75.             if (!(rec_nbr & 0X3F)) /* print rec_nbr every 48 */
  76.                 {sprintf(out, "%lu", rec_nbr);
  77.                 display(out, 21, 20, reverse);}
  78.             }
  79.         disp_err("File contains too many records", (int)rec_nbr);
  80.         fileclos(fh); 
  81.         continue;
  82.         } /* end of main for loop */
  83.     } /* end of function getedit*/
  84. /*
  85. *
  86. *    Function editfile
  87. *
  88. */
  89. void editfile(text, lineptr, len)
  90. char *text;
  91. int *lineptr;
  92. int *len;{
  93.     int h, top = 0, x = 1, bottom;
  94.   /*    setup */    
  95.     display("\030 \031 Ctrl+Home Ctrl+End PgUp PgDn", 1, 2, low);
  96.     if (mk_wndw(TOP_LINE+2, 1, SCRN_LEN - 1, 80,
  97. "  5    10   15   20   25   30   35   40   45   50   55   60   65   70   75")
  98.             < 0) goodbye(20);
  99.     KEYMATCH[0] = 0XA38;  /* PgDn, Crs-up, Crs-rt, Tab, Shift-Tab */
  100.     KEYMATCH[1] = 0X10C;  /* PgUp, Crs-dn, Crs-lt */
  101.     KEYMATCH[4] = 0X3000;  /* F5, F6 */
  102.     KEYMATCH[7] = 0X1400; /* Ctrl+end, Ctrl+home */
  103.     KEYMATCH[2] = KEYMATCH[3] = KEYMATCH[5] = KEYMATCH[6] = 0;
  104.     disp_err("",1); /* force display of control keys */    
  105.     h = WINDOW[W_NUM].H - 1; /* lines in screen */
  106.     scrndisp(text, lineptr, top, &bottom, *len); /* initial display */  
  107.     A_TRACK = 0; INCHAR = ENTER; x = 0;
  108.   /*     main for loop */
  109.     for (;;){
  110.     switch((int)INCHAR){
  111.         case CRS_LT:
  112.         case SFT_TAB: A_TRACK = 78 - 1;
  113.         case CRS_UP: if (!(--x)) 
  114.                 if (!top) {x = 1; A_TRACK = 0;} /* at TOF */
  115.                 else {scroll(-1, 0); 
  116.                     linedisp(text, lineptr, --top, (x = 1));
  117.                     if (top + h <= bottom) bottom--;}    
  118.             break;
  119.         case ENTER: 
  120.         case CRS_RT:
  121.         case TAB: A_TRACK = 0;
  122.         case CRS_DN: if ((++x) > h) 
  123.                 if (bottom >= *len) {x = h; A_TRACK = 78 - 1;}
  124.                 else {scroll(1, 0); top++; 
  125.                     linedisp(text, lineptr, ++bottom, (x = h));}
  126.             if (INCHAR != ENTER) 
  127.                 {if (top + x > *len) {x--; A_TRACK = 78 - 1;} 
  128.                 break;}
  129.             if (top + x <= bottom) break;
  130.         case INSERT: if (*len == maxrecs) break;
  131.             insline(lineptr, top + x, *len);
  132.             scroll(-1, x - 1);
  133.             A_TRACK = 0;
  134.             text[lineptr[top + x - 1]] = 0;
  135.             *len += 1; 
  136.             break;
  137.         case DELETE: if (!*len) break;
  138.             delline(lineptr, top + x, *len);
  139.             scroll(1, x - 1);
  140.             if (*len > bottom) linedisp(text, lineptr, bottom, h);
  141.                 else {bottom--; if (x > bottom - top) x--;}
  142.             *len -= 1;
  143.             break;
  144.         case PGUP: top -= h; if (top < 0) top = 0;
  145.             scrndisp(text, lineptr, top, &bottom, *len);
  146.             break;
  147.         case PGDN: if (bottom >= *len) break;
  148.             top += h; 
  149.             scrndisp(text, lineptr, top, &bottom, *len);
  150.             if (x > bottom - top) x = bottom - top;
  151.             break;
  152.         case CTL_HOME: top = 0; x = 1;
  153.             scrndisp(text, lineptr, top, &bottom, *len);
  154.             break;
  155.         case CTL_END: top = *len - h; 
  156.             if (top < 0) top = 0; 
  157.             scrndisp(text, lineptr, top, &bottom, *len);
  158.             x = bottom - top;
  159.             break;
  160.         case QUIT: if (disp_qry("Have you finished editing")) return;
  161.             break;
  162.         default: disp_err("Invalid function key", 1); break;
  163.         } /* end switch */
  164.         set_crsr(x, 1);
  165.         show_nbr(top + x, *len);
  166.         if (accept(&text[lineptr[top+x-1]], as_typed, alt_reverse, 78, 0)
  167.             < 0) goodbye(10);
  168.         }  /* end for loop */
  169.     } /* end function editfile */
  170. /*
  171. *
  172. *    Function to display a line on screen
  173. *
  174. */
  175. void linedisp(text, lineptr, nbr, x)
  176. char *text;
  177. int *lineptr;
  178. int nbr, x;{
  179.     byte line[81];
  180.     justify(as_typed, line, &text[lineptr[nbr]], 78, 0);
  181.     display(line, x, 1, ACC_DISP);
  182.     }    
  183. /*
  184. *
  185. *    Function to re_display screen on edit
  186. *
  187. */
  188. void scrndisp(text, lineptr, top, bottom, len)
  189. char *text;
  190. int *lineptr, top, *bottom, len;{
  191.     int h = WINDOW[W_NUM].H - 1, k, x;
  192.     byte line[81];
  193.     *bottom = top + h ;
  194.     if (*bottom >= len) *bottom = len; 
  195.     for (x = 1, k = top ; x <= h  && k < *bottom ; x++, k++)
  196.         linedisp(text, lineptr, k, x);
  197.     if (x > h) return;
  198.     memset(line, 32, 78); line[78] = 0; /* blank bottom of screen */
  199.     for (; x <= h ; display(line, x++, 1, ACC_DISP));
  200.     }  /* end of function scrndisp */
  201. /*
  202. *
  203. *    Function to insert pointer in lineptr
  204. *
  205. */
  206. void insline(lineptr, k, len)
  207. int *lineptr, k, len;{
  208.     int z = len - k, save = lineptr[len];    
  209.     if (z <= 0) return; /* pointer already at EOT */
  210.     memcpy ((char*)&lineptr[k], (char*)&lineptr[k - 1], ++z * sizeof(int));
  211.         lineptr[k - 1] = save;
  212.     }
  213. /*
  214. *
  215. *    Function to delete pointer from lineptr
  216. *
  217. */
  218. void delline(lineptr, k, len)
  219. int *lineptr, k, len;{
  220.     int z = len - k, save = lineptr[k - 1];
  221.     if (!z) return; /* pointer at EOT */
  222.     memcpy((char*)&lineptr[k - 1], (char*)&lineptr[k], ++z * sizeof(int));
  223.     lineptr[len] = save;
  224.     }
  225. /*
  226. *
  227. *       Function to show current record location on screen
  228. *
  229. */
  230. void show_nbr(loc, len)
  231. int loc;
  232. int len;{
  233.     char text[16];
  234.     set_clr(99, low); /* use error status color */
  235.     sprintf(text, "%3u of %-3u", loc, len);
  236.     scrn_map(text, TOP_LINE + 1, 69); /* hairy technique */
  237.     set_clr(98, low); /* reset color */
  238.     }
  239. /*
  240. *
  241. *    Function to store new file to disc
  242. *
  243. */
  244. void putfile(fh, fname, text, lineptr, len)
  245. char *fname, *text;
  246. int fh, *lineptr, len;{
  247.     long rec_nbr = 0L;
  248.     int z;
  249.     if (fh && fileback(fname) < 1) goodbye(30);
  250.     fh = fileopen(fname, ascii, recreate);
  251.     fileinit(fh, 0, 81, 0L);
  252.     if (fh <= 0) goodbye(31);
  253.     for (z = 0 ; z < len ; z++){
  254.         strcpy(FN[fh].record, &text[lineptr[z]]); /* copy to record area */
  255.         concat(FN[fh].record, 0); /* strip trailing spaces */
  256.         strcat(FN[fh].record, "\n"); /* Add the '\n' char */
  257.         if (filewrit(fh, &rec_nbr)) goodbye(32);
  258.         if (!(rec_nbr & 0XF)) 
  259.             disp_msg("Writing to disc - record",(int)rec_nbr);
  260.         }
  261.     disp_msg("",0);
  262.     fileclos(fh);
  263.     rm_wndw();
  264.     }
  265. /*
  266. *
  267. *    Main program
  268. *
  269. */
  270. void main(){
  271. int len = 0;
  272. int fh, *lineptr;
  273. char *text, fname[66];
  274. /*  setup */
  275. clr_scrn("Text Editor");
  276. text = malloc(maxsize); /* assign workarea */
  277. lineptr = (int*) malloc(sizeof(int) * maxrecs);
  278. if (text == NULL || lineptr == NULL) goodbye(1);
  279. /*    edit */
  280. if ((fh = getfile(fname, text, lineptr, &len)) < 0) goodbye(2);
  281. editfile(text, lineptr, &len);
  282. putfile(fh, fname, text, lineptr, len);
  283. disp_err("ready for goodbye", 11); idleloop(18);
  284. goodbye(0);
  285. }
  286.